package com.dhu.graph.basic.GraphImpls;

import com.dhu.graph.basic.AbstractGraph;
import com.dhu.graph.basic.edge.Edge;
import com.dhu.graph.utils.Bag;

/**
 * @ Author ：snow
 * @ Date   ：Created in 2020-05-08
 * @ Description：
 * @ Modified By：
 * @ Version:
 */
public class EdgeWeightGraph implements AbstractGraph {
    private int V;
    private int E;
    private Bag<Edge>[] adj;

    public EdgeWeightGraph(int v){
        this.V = v;
        this.adj = (Bag<Edge>[]) new Bag[v];
        for (int i = 0; i < v; i++) {
            adj[i] = new Bag<Edge>();
        }
    }
    @Override
    public void addEdge(Edge e){
        int v = e.either();
        int w = e.other(v);

        this.adj[v].put(e);
        this.adj[w].put(e);
        this.E ++;
    }
    @Override
    public Iterable<Edge> adj(int v){
        return this.adj[v];
    }

    @Override
    public void addEdge(int v, int w) {
        throw new RuntimeException("This method only can be call in  Not Weight Graph,cause in this Graph edge is an Object");
    }
    @Override
    public int V(){
        return this.V;
    }
    @Override
    public int E(){
        return this.E;
    }
    public Iterable<Edge> edges(){
        Bag<Edge> edges = new Bag<>();
        for (int i = 0; i < V; i++){
            for (Edge edge : adj[i]) {
                edges.put(edge);
            }
        }
        return edges;
    }
}
